package com.kye.express.utils;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;

import com.jakewharton.rxbinding2.support.v7.widget.RxToolbar;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.business.base.BaseAppCompatActivity;

import io.reactivex.functions.Consumer;

public class ToolbarManager {
    public static void setToolbar(BaseAppCompatActivity baseAppCompatActivity, Toolbar toolbar) {
        if (toolbar != null) {
            baseAppCompatActivity.setSupportActionBar(toolbar);
            ActionBar actionBar = baseAppCompatActivity.getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
            }

            RxToolbar.navigationClicks(toolbar)
                    .compose(new DebounceObservableTransformer<>())
                    .compose(baseAppCompatActivity.bindToLifecycle())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            baseAppCompatActivity.onBackPressed();
                        }
                    });
        }
    }
}
